package com.jgraph.pad.persistence;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import com.jgraph.editor.JGraphEditorResources;
import com.jgraph.editor.persistence.JGraphPersistenceManager;

/**
 * This is the base implementation for persistence managers in the application.
 *
 * @author svetoslavganov@mail.utexas.edu
 *
 */
public abstract class JGraphpadAbstractPersistenceManager<T>
        implements JGraphPersistenceManager<T> {

	public abstract T readObject(String uri)
	        throws MalformedURLException, IOException;

	public abstract void writeObject(T object, String uri)
	        throws MalformedURLException, IOException;

	public InputStream getInputStream(String uri) throws MalformedURLException,
			FileNotFoundException, IOException {
		InputStream in = JGraphEditorResources.getInputStream(uri.toString());
		if (uri.toLowerCase().endsWith(".gz"))
			in = new GZIPInputStream(in);
		return new BufferedInputStream(in);
	}

	public OutputStream getOutputStream(String uri) throws IOException {
		OutputStream out = JGraphEditorResources.getOutputStream(uri);
		if (uri.toLowerCase().endsWith(".gz"))
			out = new GZIPOutputStream(out);
		return out;
	}
}
